package org.judking.CarKeeper.command.service;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.judking.CarKeeper.activity.MainActivity;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.test.IsolatedContext;
import android.util.Log;

public class UploadService {
	public static final String UPLOAD_URL = "http://192.168.1.105/JMuners/test/test/";
	public static ConnectivityManager connectivityManager = null;
	public static Handler handler = null;

	public static void run() {
		new Thread(new Runnable() {

			@Override
			public void run() {
				if (isOnline()) {
					Upload();
				} else {
					Message msg = handler
							.obtainMessage(MainActivity.MESSAGE_TOAST);
					Bundle bundle = new Bundle();
					bundle.putString(MainActivity.TOAST,
							"not connecting to internet");
					msg.setData(bundle);
					handler.sendMessage(msg);
				}
			}
		}).start();
	}

	private synchronized static void Upload() {
		HttpClient client = new DefaultHttpClient();
		String[] lists = null;
		try {
			String data = SynFileOpreService.synGetNClear();
			lists = data.split(JsonLogger.DELIM);
		} catch (IOException e) {
			Message msg = handler.obtainMessage(MainActivity.MESSAGE_TOAST);
			Bundle bundle = new Bundle();
			bundle.putString(MainActivity.TOAST, "read cache fail");
			msg.setData(bundle);
			handler.sendMessage(msg);
		}
		
		int errCnt = 0;
		for (int i = 0; i < lists.length; ++i) {
			HttpPost httpPost = new HttpPost(UPLOAD_URL);
			List<BasicNameValuePair> info = new ArrayList<BasicNameValuePair>();
			info.add(new BasicNameValuePair("list", lists[i]));
			try {
				httpPost.setEntity(new UrlEncodedFormEntity(info));
				client.execute(httpPost);
			} catch (IOException e) {
				if(errCnt == 2)		{
					errCnt = 0;
					try {
						SynFileOpreService.synAppend(lists[i]+JsonLogger.DELIM);
					} catch (IOException e1) {}
					Message msg = handler.obtainMessage(MainActivity.MESSAGE_TOAST);
					Bundle bundle = new Bundle();
					bundle.putString(MainActivity.TOAST, "upload fail: "+e.getMessage());
					msg.setData(bundle);
					handler.sendMessage(msg);
				}
				else		{
					errCnt++;
					i--;
					Log.e("judking", "upload : ioexception: "+e.getMessage());
				}
			}
			
		}
		Message msg = handler.obtainMessage(MainActivity.MESSAGE_TOAST);
		Bundle bundle = new Bundle();
		bundle.putString(MainActivity.TOAST, "upload success");
		msg.setData(bundle);
		handler.sendMessage(msg);

	}

	private static boolean isOnline() {
		NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
		if (netInfo != null && netInfo.isConnectedOrConnecting()) {
			return true;
		}
		return false;
	}
}
